<?php

namespace AdminBundle\Controller;

use AdminBundle\Entity\BaseDataSource;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use AdminBundle\Utility\Constant;

/**
 * Basedatasource controller.
 *
 * @Route("basedatasource")
 */
class BaseDataSourceController extends Controller
{
    /**
     * Lists all baseDataSource entities.
     *
     * @Route("/", name="basedatasource_index")
     * @Method("GET")
     */
    public function indexAction(Request $request)
    {
        $page = $request->get("page");
        $page = is_numeric($page) ? $page : 1;
        $keyword = $request->get('keyword');
        $date_range = $request->get('date_range');
        if (!empty($date_range)) {
            $date_range = explode(" - ", $date_range);
            $starttime = $date_range[0];
            $endtime = $date_range[1];
        } else {
            $starttime = $endtime = null;
        }

        $starttime = date("Y-m-d", strtotime($starttime ?: "-3 day"));
        $endtime = date("Y-m-d", strtotime($endtime ?: "+1 day"));

        $params = ['keyword' => $keyword, 'starttime' => $starttime, 'endtime' => $endtime];

        $result = $this->getDoctrine()->getRepository('AdminBundle:BaseDataSource')->pageQuery($params, $page);

        return $this->render('AdminBundle:basedatasource:index.html.twig', array_merge([
            'dataSources' => $result['list'],
            'querystring' => http_build_query($params),
            'page' => $result['page'],
        ], $params));
    }

    /**
     * Creates a new baseDataSource entity.
     *
     * @Route("/new", name="basedatasource_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {
        $baseDataSource = new Basedatasource();
        $form = $this->createForm('AdminBundle\Form\BaseDataSourceType', $baseDataSource, Constant::FORM_ATTR_CLASS);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $baseDataSource->setCtime(new \DateTime())
                ->setCreator($this->getUser()->getUsername())
                ->setIp($request->getClientIp());

            $em = $this->getDoctrine()->getManager();
            $em->persist($baseDataSource);
            $em->flush();

            return $this->redirectToRoute('basedatasource_show', array('id' => $baseDataSource->getId()));
        }

        return $this->render('AdminBundle:basedatasource:new.html.twig', array(
            'baseDataSource' => $baseDataSource,
            'form' => $form->createView(),
        ));
    }

    /**
     * Finds and displays a baseDataSource entity.
     *
     * @Route("/{id}", name="basedatasource_show")
     * @Method("GET")
     */
    public function showAction(BaseDataSource $baseDataSource)
    {
        $deleteForm = $this->createDeleteForm($baseDataSource);

        return $this->render('AdminBundle:basedatasource:show.html.twig', array(
            'dataSource' => $baseDataSource,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing baseDataSource entity.
     *
     * @Route("/{id}/edit", name="basedatasource_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, BaseDataSource $baseDataSource)
    {
        $deleteForm = $this->createDeleteForm($baseDataSource);
        $editForm = $this->createForm('AdminBundle\Form\BaseDataSourceType', $baseDataSource, Constant::FORM_ATTR_CLASS);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('basedatasource_edit', array('id' => $baseDataSource->getId()));
        }

        return $this->render('AdminBundle:basedatasource:edit.html.twig', array(
            'dataSource' => $baseDataSource,
            'form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a baseDataSource entity.
     *
     * @Route("/{id}", name="basedatasource_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, BaseDataSource $baseDataSource)
    {
        $form = $this->createDeleteForm($baseDataSource);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->remove($baseDataSource);
            $em->flush();
        }

        return $this->redirectToRoute('basedatasource_index');
    }

    /**
     * Creates a form to delete a baseDataSource entity.
     *
     * @param BaseDataSource $baseDataSource The baseDataSource entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm(BaseDataSource $baseDataSource)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('basedatasource_delete', array('id' => $baseDataSource->getId())))
            ->setMethod('DELETE')
            ->getForm();
    }
}
